package com.ucas.kafka.producer;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import com.ucas.kafka.protobuf.AlarmEntity;
import com.ucas.kafka.protobuf.Lns;
import com.ucas.kafka.protobuf.Lns.EuEntity;
import com.ucas.kafka.protobuf.Lns.ReplayResponse;
import com.ucas.kafka.protobuf.TCPMsgEntity.CmdEntity;
import com.ucas.kafka.protobuf.TCPMsgEntity.TcpCmdExecState;


@Component
@EnableScheduling
public class KafkaProducer {
	@Autowired
	KafkaTemplate<String,byte[]> kafkaTemplate;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	int index = 1;
	/**
	 * 定时任务： fixedDelay 上次任务结束和下次任务执行开始之间的时间间隔。
	 * 		   fixedRate  每隔多长时间执行一次任务，不管上次任务是否执行完成，都在这个时间之后开始新的任务。
	 * 定时任务时间单位：毫秒	
	 */
	@Scheduled(fixedDelay = 2*1000)
	public void sendMessage() {
//		sendAlarmMonitoring();
//		sendAlarmMonitoringDelay();
//		sendTcpCmd();
		sendEngbc();
		index++;
	}
	/**
	 * 发送报警监控列表信息
	 */
	public void sendAlarmMonitoring() {
		//固定new对象的方法，，由protoBuffer定义，，，，
		AlarmEntity.AlarmResp.Builder alarmResp = AlarmEntity.AlarmResp.newBuilder();
		alarmResp.setResponseMemo("AlarmResp.Memo");
		alarmResp.setResponseType("AlarmResp.Type");
		alarmResp.setResponseTime(System.currentTimeMillis());
		alarmResp.setResponseUsename("AlarmResp.Usename");
		
		AlarmEntity.RuleIden.Builder ruleiden = AlarmEntity.RuleIden.newBuilder();
		ruleiden.setId(1492563757000L);
		ruleiden.setName("RuleIden.name");
		
		AlarmEntity.ProccessIden.Builder proccessIden = AlarmEntity.ProccessIden.newBuilder();
		proccessIden.setName("ProccessIden---name");
		
		AlarmEntity.Alarm.Builder alarm = AlarmEntity.Alarm.newBuilder();
		alarm.setAlarmValue("1");
		alarm.setBeginTime(System.currentTimeMillis());
		alarm.setContent("报警测试---高亮显示");
//		alarm.setEndTime(System.currentTimeMillis());
		alarm.setId(index);
		alarm.setLevel(2);
		
		alarm.setRule(ruleiden);
		alarm.setResponse(alarmResp);
		alarm.setHandler(proccessIden);
		
		byte[] alarmByte = alarm.build().toByteArray();
		kafkaTemplate.send("alarm", alarmByte);
	}
	/**
	 * 发送延时遥测报警监控列表信息
	 */
	public void sendAlarmMonitoringDelay() {
		AlarmEntity.AlarmResp.Builder alarmResp = AlarmEntity.AlarmResp.newBuilder();
		alarmResp.setResponseMemo("AlarmResp.Memo");
		alarmResp.setResponseType("AlarmResp.Type");
		alarmResp.setResponseTime(System.currentTimeMillis());
		alarmResp.setResponseUsename("AlarmResp.Usename");
		
		AlarmEntity.RuleIden.Builder ruleiden = AlarmEntity.RuleIden.newBuilder();
		ruleiden.setId(1492563757000L);
		ruleiden.setName("RuleIden.name");
		
		AlarmEntity.ProccessIden.Builder proccessIden = AlarmEntity.ProccessIden.newBuilder();
		proccessIden.setName("ProccessIden---name");
		
		AlarmEntity.AlarmDelay.Builder alarm = AlarmEntity.AlarmDelay.newBuilder();
		alarm.setAlarmValue("1");
		alarm.setBeginTime(System.currentTimeMillis());
		alarm.setContent("报警测试---高亮显示");
		alarm.setId(index);
		alarm.setLevel(2);
		alarm.setCreateTime(System.currentTimeMillis());
		
		alarm.setRule(ruleiden);
		alarm.setResponse(alarmResp);
		alarm.setHandler(proccessIden);
		
		byte[] alarmByte = alarm.build().toByteArray();
		kafkaTemplate.send("alarm_replay", alarmByte);
	}
	/**
	 * 发送指令回令信息
	 */
	public void sendTcpCmd() {
		//指令回令信息---列表数据
		TcpCmdExecState.Builder tces = TcpCmdExecState.newBuilder();
		tces.setLen(10);
		tces.setSatid("APSTAR09");
		tces.setData("Data-1234");
		CmdEntity.Builder ce = CmdEntity.newBuilder();
		if(index%2 == 0) {
			ce.setCmdid("cmd-"+(index-1));
		}else {
			ce.setCmdid("cmd-"+index);
		}
		tces.setCmd(ce);
		tces.setType("KACK");
		if(index%4 == 0) {
			tces.setState("0");
		}else {
			Random ran = new Random();
			tces.setState(ran.nextInt(9)+"");
		}
		byte[] tcesbyte = tces.build().toByteArray();
		kafkaTemplate.send("vela-tcpmsg",tcesbyte);
	}
	/**
	 * 遥测参数观察器
	 */
	public void sendEngbc() {
		//遥测参数观察器--列表数据
		Lns.ReplayResponse.Builder rr = Lns.ReplayResponse.newBuilder();
		rr.setEor(true);
		rr.setCid(3);
		rr.setTime(System.currentTimeMillis());
		//封装Response的内容
		getResponse(rr);
		//发送消息给kafka
		kafkaTemplate.send("engbc",rr.build().toByteArray());
	}
	public void getResponse(ReplayResponse.Builder builder) {
		
		EuEntity.Builder e262281 = EuEntity.newBuilder();
		e262281.setDisplay("帧计数");
		Random voltagerandom = new Random();
		e262281.setVoltage(Double.doubleToLongBits(Double.NaN));
		e262281.setTmid(262146);
		e262281.setRaw(100L);
		
		EuEntity.Builder e268802 = EuEntity.newBuilder();
		e268802.setDisplay("测控接收机A1 AGC电压");
		e268802.setVoltage(Double.doubleToLongBits(Double.NaN));
		e268802.setTmid(268802);
		e268802.setRaw(100L);
		
		
		EuEntity.Builder e268803 = EuEntity.newBuilder();
		BigDecimal   e268803b   =   new   BigDecimal(voltagerandom.nextDouble()*10);   
		e268803.setDisplay("测控接收机A1 频差电压");
		e268803.setVoltage(Double.doubleToLongBits(e268803b.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue()));
		e268803.setTmid(268803);
		e268803.setRaw(100L);
		e268803.setEu(Double.doubleToLongBits(e268803b.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue()));
		
		
		EuEntity.Builder e262650 = EuEntity.newBuilder();
		BigDecimal   e262650b   =   new   BigDecimal(voltagerandom.nextDouble()*10);   
		e262650.setDisplay("X轴控制器计算力矩(Mx)");
		e262650.setVoltage(Double.doubleToLongBits(e262650b.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue()));
		e262650.setTmid(262650);
		e262650.setRaw(100L);
		e262650.setEu(Double.doubleToLongBits(e262650b.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue()));
		
		EuEntity.Builder e262651 = EuEntity.newBuilder();
		BigDecimal    e262651b   =   new   BigDecimal(voltagerandom.nextDouble()*10); 
		e262651.setDisplay("Y轴控制器计算力矩(My)");
		e262651.setVoltage(Double.doubleToLongBits(e262651b.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue()));
		e262651.setTmid(262651);
		e262651.setRaw(100L);
		e262651.setEu(Double.doubleToLongBits(e262650b.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue()));
		
		builder.addEu(e262281);
		builder.addEu(e268802);
		builder.addEu(e268803);
		builder.addEu(e262650);
		builder.addEu(e262651);
	}
}
